<template>
  <div class="box d_f f_d_c a_i_c p_r">
    <img src="../../assets/back.png" alt="" class="back" @click="back" />
    <steps :active="active" :fields="fields"></steps>
    <div v-if="active == 0">
      <div class="d_f tabs a_i_c">
        <el-tabs :tab-position="tabPosition" @tab-change="tabChange" v-model="tabName" style="height: 200px"
          class="demo-tabs">
          <el-tab-pane label="密码认证" name="0"></el-tab-pane>
          <el-tab-pane label="邮箱认证" name="1"></el-tab-pane>
        </el-tabs>

        <div v-if="tabName == '0'" class="d_f f_d_c a_i_c left">
          <div class="d_f a_i_c">
            <div class="label">原密码</div>
            <div class=" m_l_10 codeInput">
              <el-input v-model="password" clearable type="password" placeholder="请输入原始密码" show-password />
            </div>
          </div>
          <div class="button d_f j_c_c a_i_c" @click="authentication(2)">立即认证</div>
        </div>
        <div v-if="tabName == '1'" class="d_f f_d_c a_i_c left">
          <div class="d_f a_i_c">
            <div class="label t_a_l">邮箱</div>
            <div class="  m_l_10 codeInput">
              {{ email }}
            </div>
          </div>
          <div class="d_f a_i_c m_t_10">
            <div class="label">验证码</div>
            <div class=" m_l_10 p_r d_f a_i_c codeInput">
              <el-input v-model="emailCode" clearable :maxlength="6" placeholder="请输入验证码" />
              <span class="code" v-if="flag">{{ code }}秒</span>
              <span class="code" @click="sendCode" v-else>获取验证码</span>
            </div>
          </div>
          <div class="button d_f j_c_c a_i_c" @click="authentication(1)">立即认证</div>
        </div>
      </div>

    </div>
    <div v-if="active == 1" class="tabs d_f f_d_c a_i_c">
      <div class="d_f a_i_c m_t_10">
        <div class="label width">新手机号</div>
        <div class=" m_l_10  d_f a_i_c codeInput">
          <el-input v-model="newMobile" type="number" clearable placeholder="请输入新手机号" />
        </div>
      </div>
      <div class="button d_f j_c_c a_i_c" @click="modifyNow">立即修改</div>
    </div>
    <div class="tabs d_f f_d_c a_i_c" v-if="active == 2">
      <div class="d_f j_c_c a_i_c p_r">
        <img src="../../assets/success_icon1.png" alt="" class="icon1">
        <img src="../../assets/success_icon2.png" alt="" class="icon2">
      </div>
      <h5 class="size">手机号修改成功</h5>
      <div class="button d_f j_c_c a_i_c" @click="back">返回个人中心</div>
    </div>
  </div>
</template>

<script lang='ts'>
import { defineComponent, reactive, toRefs, onBeforeMount, onBeforeUnmount, onMounted, onUnmounted, onUpdated, onBeforeUpdate } from 'vue'
import { phoneType } from '../../type/personal'
import steps from '../../components/steps.vue'
import { phoneNumFilter, emailFilter } from '../../utils/date'
import { useRouter } from 'vue-router'
import { empValidCode, empSendMail, empResetKey } from '../../api/login'
import md5 from "js-md5"
import { Message } from '../../utils/resetMessage'
import { useStore } from '../../store/index';
import {PHONE_REG} from '../../utils/regular'
export default defineComponent({
  name: '',
  components: {
    steps
  },
  setup() {
    const data: phoneType = reactive({
      active: 0,
      tabPosition: 'left',
      tabName: '0',
      password: '',
      fields: [
        {
          title: '认证身份',
          doc: '认证您的身份信息'
        },
        {
          title: '更换手机',
          doc: '输入新的手机号'
        },
        {
          title: '完成修改',
          doc: '新手机号生效'
        },
      ],
      phone: '',
      email: '',
      code: 59,
      flag: false,
      timer: null,
      emailCode: '',
      newMobile: '',
    })
    const router = useRouter()
    const store = useStore();
    onBeforeMount(() => {
      if (localStorage.getItem('userVO')) {
        data.email = JSON.parse(<string>localStorage.getItem('userVO')).email
        // data.phone = phoneNumFilter('17311701400')
      }

      // data.email = emailFilter('1414727570@qq.com')
    })
    onMounted(() => {
    })
    onBeforeUpdate(() => {
    })
    onUpdated(() => {
    })
    onUnmounted(() => {
    })
    onBeforeUnmount(() => {
    })
    //发送验证码
    const sendCode = (): void => {
      empSendMail({ email: data.email }).then((res: any) => {
        Message({ type: 'success', message: '发送成功' })
        data.flag = true
        data.timer = setInterval(() => {
          if (data.code > 1) {
            data.code--
          } else {
            data.flag = false
            clearInterval(data.timer)
            data.timer = null
            data.code = 59
          }
        }, 1000)
      })
    }
    //点击tab切换清楚定时器跟验证码秒数
    const tabChange = (): void => {
      clearInterval(data.timer)
      data.timer = null
      data.flag = false
      data.code = 59
    }
    const getRandomString = (length: number) => {
      let result = '';
      const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
      const charactersLength = characters.length;
      for (let i = 0; i < length; i++) {
        result += characters.charAt(Math.floor(Math.random() * charactersLength));
      }
      return result;
    }
    //密码跟验证码验证
    const authentication = (validType: number): void => {
      let salt = 'KXY-9935'
      let password = md5(data.password + salt) + getRandomString(10)
      empValidCode({ validType: validType, oldPwd: password, email: data.email, code: data.emailCode }).then((res: any) => {
        data.active = 1
      })
    }
    //修改密码提交事件
    const modifyNow = (): void => {
      if (!data.newMobile) {
        Message({ type: 'error', message: '请输入新手机号' })
        return
      }
     if(!PHONE_REG.test(data.newMobile)){
      Message({ type: 'error', message: '手机号格式不正确' })
        return
     }
      empResetKey({ modifyType: 1, newMobile: data.newMobile }).then((res: any) => {
        Message({ type: 'success', message: '手机号修改成功' })
        data.active = 2
        store.getUser()
      })
    }
    const back = (): void => {
      router.push('/personal/index')
    }
    return {
      ...toRefs(data),
      sendCode, tabChange,
      authentication, modifyNow,
      back
    }
  }
})
</script>
<style scoped lang='scss'>
.box {
  background: linear-gradient(175deg, #F5F9FF 7%, #FFFFFF 61%);
  width: 100%;
  height: calc(100% - 96px);
  padding: 48px 0px;
  border: 1px solid #d9e1ec;
  border-radius: 6px;
}

.tabs {
  margin-top: 100px;
}

.label {
  color: rgba(0, 0, 0, 0.9);
  font-size: 14px;
  width: 45px;
}

.width {
  width: 70px;
  text-align: right;
}

.button {
  box-shadow: 0px 8px 16px 0px rgba(13, 97, 216, 0.2);
  background: #1878FF;
  padding: 0px 16px;
  height: 32px;
  border-radius: 3px;
  color: #FFFFFF;
  font-size: 14px;
  margin-top: 24px;
  cursor: pointer;
}

.left {
  margin-left: 56px;
}

.phone {
  width: 163px;
}

.code {
  color: rgba(0, 0, 0, 0.9);
  font-size: 12px;
  position: absolute;
  right: 10px;
  cursor: pointer;
}

.icon1 {
  width: 56px;
  height: 56px;
}

.icon2 {
  width: 60px;
  // height: 22px;
  margin-top: 8px;
  position: absolute
}

.size {
  margin-top: 16px;
}

.back {
  position: absolute;
  left: 24px;
  top: 24px;
  width: 24px;
  height: 24px;
  cursor: pointer;
}
</style>
<style lang='scss'>
.codeInput {
  width: 280px !important;
.el-input {
  width: 280px !important;
}
}
</style>